package com.ctl.lib_common.widget.recyclerview;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

/**
 * created by : chentl
 * Date: 2020/07/28
 */
public class MyDecoration extends RecyclerView.ItemDecoration {

    private Drawable mDivider;
    private int mLeft = 0;
    private int mTop = 0;
    private int mRight = 0;
    private int mBottom;

    public MyDecoration() {
        this(Color.parseColor("#eeeeee"));
    }

    public MyDecoration(int color) {
        this(color, 1);
    }

    public MyDecoration(int color, int bottom) {
        this.mDivider = new ColorDrawable(color);
        mBottom = bottom;
    }

    @Override
    public void onDraw(@NotNull Canvas c, RecyclerView parent, @NotNull RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount - 1; i++) {
            final View child = parent.getChildAt(i);
            //获得child的布局信息
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mBottom;
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, @NotNull View view, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
        outRect.set(mLeft, mTop, mRight, mBottom);
    }

    public void setDivider(Drawable mDivider) {
        this.mDivider = mDivider;
    }

    public void setLeft(int left) {
        this.mLeft = left;
    }

    public void setTop(int top) {
        this.mTop = top;
    }

    public void setRight(int right) {
        this.mRight = right;
    }

    public void setBottom(int bottom) {
        this.mBottom = bottom;
    }
}
